<template>
  <view class="goods-detail-container" v-if="goods_detail">
    <!-- 轮播图 -->
    <swiper indicator-dots autoplay circular>
      <swiper-item v-for="item in goods_detail.pics" :key="item.pics_id">
        <image
          :src="item.pics_big"
          mode="scaleToFill"
          @click="preview(item.pics_big)"
        />
      </swiper-item>
    </swiper>
    <!-- 商品信息区域 -->
    <view class="goods-info-box">
      <!-- 商品价格 -->
      <view class="price">￥{{ goods_detail.goods_price }}</view>
      <!-- 信息主体区域 -->
      <view class="goods-info-body">
        <!-- 商品名称 -->
        <view class="goods-name">{{ goods_detail.goods_name }}</view>
        <!-- 收藏 -->
        <view class="favi">
          <uni-icons type="star" size="18" color="gray"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <!-- 运费 -->
      <view class="yf">快递: 免运费</view>
    </view>
    <!-- 图文列表 -->
    <!-- <view v-html="goods_detail.goods_introduce"></view> -->
    <rich-text :nodes="goods_detail.goods_introduce"></rich-text>
    <!-- 商品导航 -->
    <view class="goods_nav">
      <uni-goods-nav
        :fill="true"
        :options="options"
        :buttonGroup="buttonGroup"
        @click="onClick"
        @buttonClick="buttonClick"
      />
    </view>
  </view>
</template>

<script>
// 导入vuex辅助函数
import { mapGetters, mapMutations } from "vuex";

export default {
  data() {
    return {
      goods_detail: null, // 商品详情数据
      options: [
        {
          icon: "shop",
          text: "店铺",
          info: 0,
          infoBackgroundColor: "#ff0000",
          infoColor: "#fff",
        },
        {
          icon: "cart",
          text: "购物车",
          info: 0,
        },
      ],
      buttonGroup: [
        {
          text: "加入购物车",
          backgroundColor: "#ff0000",
          color: "#fff",
        },
        {
          text: "立即购买",
          backgroundColor: "#ffa200",
          color: "#fff",
        },
      ],
    };
  },
  onLoad(options) {
    this.getGoodsDetailData(options.goods_id);
    // console.log(this.getTotal);
    // 把仓库中的商品总数赋值给徽标
    this.options[1].info = this.getTotal;
  },
  computed: {
    ...mapGetters("cart", ["getTotal"]),
  },
  methods: {
    ...mapMutations("cart", ["addToCart"]),
    // 发请求获取数据
    async getGoodsDetailData(goods_id) {
      const res = await uni.$request({
        url: "goods/detail",
        data: {
          goods_id,
        },
      });
      // console.log(res);
      if (res.meta.status === 200) {
        res.message.goods_introduce = res.message.goods_introduce
          .replace(/<img/g, '<img style="display:block;"')
          .replace(/.webp/g, ".jpg");
        this.goods_detail = res.message;
      }
    },
    preview(current) {
      uni.previewImage({
        current, // 当前显示图片的http链接
        urls: this.goods_detail.pics.map((item) => item.pics_big), // 需要预览的图片http链接列表
      });
    },
    // 加入购物车
    onClick(e) {
      if (e.content.text === "购物车") {
        uni.switchTab({
          url: "/pages/cart/index",
        });
      }
    },
    buttonClick(e) {
      // console.log(e);
      if (e.content.text === "加入购物车") {
        const goods = {
          goods_id: this.goods_detail.goods_id,
          goods_name: this.goods_detail.goods_name,
          goods_price: this.goods_detail.goods_price,
          goods_small_logo: this.goods_detail.goods_small_logo,
          goods_number: 1, // 每次加入购物车的数量为1
          goods_state: true, // 购物车中商品的选中状态，默认为选中
        };
        // console.log(goods);
        this.addToCart(goods);

        uni.showToast({
          title: "加入购物车成功~",
          duration: 800,
        });
        // 加入购物车，把仓库中最新的总数，重新赋值给徽标
        this.options[1].info = this.getTotal;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.goods-detail-container {
  // 给页面外层的容器，添加 50px 的内padding，
  // 防止页面内容被底部的商品导航组件遮盖
  padding-bottom: 50px;
  swiper {
    height: 750rpx;

    image {
      width: 100%;
      height: 100%;
    }
  }
  // 商品信息区域的样式
  .goods-info-box {
    padding: 20px;
    padding-right: 0;

    .price {
      color: #c00000;
      font-size: 18px;
      margin: 10px 0;
    }

    .goods-info-body {
      display: flex;
      justify-content: space-between;

      .goods-name {
        font-size: 16px;
        line-height: 25px;
        padding-right: 10px;
      }
      // 收藏区域
      .favi {
        width: 120px;
        font-size: 14px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        border-left: 1px solid #efefef;
        color: gray;
      }
    }

    // 运费
    .yf {
      margin: 10px 0 17px;
      font-size: 14px;
      color: gray;
    }
  }
  .goods_nav {
    // 为商品导航组件添加固定定位
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
  }
}
</style>
